package com.zt.rongda.bus.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zt.rongda.bus.inter.BusMapper;
import com.zt.rongda.bus.model.Bus;
import com.zt.rongda.constant.BusConstant;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusInfoSyncService extends BaseService {

    @Autowired
    private BusMapper busMapper;

    @Autowired
	private WuhanbusAPIService wuhanbusAPIService;

    public void loadBusInfo() throws Exception {
        Date now = new Date();
        
        JSONArray newDatas = wuhanbusAPIService.getBuses();
        if(newDatas == null || newDatas.isEmpty()){
    		return;
    	}
        List<Bus> dbBuses = busMapper.selectAllBuses();
        Map<String, Bus> dbMaps = new HashMap<String, Bus>();
        if (dbBuses != null && !dbBuses.isEmpty()) {
            for (Bus o : dbBuses) {
                dbMaps.put(o.getBusId(), o);
            }
        }
        
        String id, code, reg_code, company_id, company_name, line_id, line_name, is_producing, fuel_type, bus_body_type, veh_level = null;

        for (Object o : newDatas) {
            JSONObject _new = (JSONObject) o;
			id = noNull(_new.getString("id")); //id
			code = noNull(_new.getString("code")); //自编号
			reg_code = noNull(_new.getString("reg_code")); //车牌号
            company_id = noNull(_new.getString("company_id"));
			company_name = noNull(_new.getString("company_name"));
			line_id = noNull(_new.containsKey("line_id") ? _new.getString("line_id") : "");
			line_name = noNull(_new.getString("line_name"));
			is_producing = noNull(_new.getString("is_producing")); //是否为营运车辆
			fuel_type = noNull(_new.getString("fuel_type")); //燃料类型 能源类型

			bus_body_type = noNull(_new.getString("bus_body_type")); //车身类型 单双层

			veh_level = noNull(_new.getString("veh_level")); //车辆级别 空调车

            Bus dbBus = dbMaps.remove(id);
            if (dbBus != null) {// 数据库存在
                if (!company_id.equals(dbBus.getCompanyId()) || !company_name.equals(dbBus.getCompanyName())
                        || !line_name.equals(dbBus.getLineNo()) || !code.equals(dbBus.getVehicleNo())
                        || !reg_code.equals(dbBus.getPlateNo()) || !fuel_type.equals(dbBus.getBusEnergy())
						|| !bus_body_type.equals(dbBus.getBusFloor()) || !veh_level.equals(dbBus.getAirConditioned())) {// 信息变更
                    dbBus.setVehicleNo(code);
                    dbBus.setVehicleNoAbbr(this.getVehicleAbbrNo(code));
                    dbBus.setPlateNo(reg_code);
                    dbBus.setLineId(line_id);
                    dbBus.setLineNo(line_name);
                    dbBus.setCompanyId(company_id);
                    dbBus.setCompanyName(company_name);
					dbBus.setBusEnergy(fuel_type);
					dbBus.setAirConditioned(veh_level);
					dbBus.setBusFloor(bus_body_type);
					
                    dbBus.setModifyTime(now);
                    dbBus.setDataDate(now);
                    busMapper.updateByPrimaryKey(dbBus);
                }
            } else if("1".equals(is_producing) || "True".equals(is_producing)){// 营运车辆
                Bus bus = new Bus();
                bus.setBusId(id);
                bus.setVehicleNo(code);
                bus.setVehicleNoAbbr(this.getVehicleAbbrNo(code));
                bus.setPlateNo(reg_code);
                bus.setLineId(line_id);
                bus.setLineNo(line_name);
                bus.setCompanyId(company_id);
                bus.setCompanyName(company_name);
                bus.setBusEnergy(fuel_type);
                bus.setAirConditioned(veh_level);
                bus.setBusFloor(bus_body_type);
                
                bus.setStatus(BusConstant.Bus.BUS_STATUS_TRUE);
                bus.setMakeTime(now);
                bus.setModifyTime(now);
                bus.setDataDate(now);
                busMapper.insert(bus);
            }
        }
    }

    private String getVehicleAbbrNo(String vehicleNo) {
        String vehicleAbbrNo = "";
        if (!StringUtil.isEmpty(vehicleNo)) {
            String[] s = vehicleNo.replaceAll("[^0-9]", "#").split("#");
            vehicleAbbrNo = s[s.length - 1];
        }
        return vehicleAbbrNo;
    }

    public static final String noNull(String string, String defaultString) {
        return StringUtil.isEmpty(string) ? defaultString : string;
    }

    public static final String noNull(String string) {
        return noNull(string, "");
    }
}
